home *** CD-ROM | disk | FTP | other *** search
- mailx Revision: 64.64 Date: 89/07/24 09:21:08 Type ? for help.
- "/usr/mail/dc1ik": 1 message 1 new
- >N 1 dk5sg!deyke Fri May 31 18:09 224/5052 Re: trunc.c?
- mailx> "/users/funk/dc1ik/mbox" [Appended] 224/5052
- mailx> Message 1:
- From dk5sg!deyke Fri May 31 18:09 MES 1991
- Received: from dk5sg by db0sao; Fri, 31 May 91 18:09:43 mes
- Return-Path: <dk5sg!deyke>
- Received: by mdddhd.HP.COM; Fri, 31 May 91 10:09:13 mdt
- From: Dieter Deyke <mdddhd!deyke>
- Full-Name: Dieter Deyke
- Message-Id: <9105311609.AA19464@mdddhd.HP.COM>
- Subject: Re: trunc.c?
- To: dc1ik (Olaf Erb)
- Date: Fri, 31 May 91 10:09:11 MDT
- In-Reply-To: <m0jjDgo-0001XeC@rnieph.rni.sub.org>; from "Olaf Erb" at May 31, 91 5:55 pm
- Reply-To: deyke@hpfcmdd.fc.hp.com
- Mailer: Elm [revision: 64.9]
- Status: R
-
- > Hallo Dieter,
- > habe bei Dir irgendwo trunc.c,s o. ae. gesehen, hast Du den Src davon?
- > 73s
- > Olaf
-
- #---------------------------------- cut here ----------------------------------
- # This is a shell archive. Remove anything before this line,
- # then unpack it by saving it in a file and typing "sh file".
- #
- # Wrapped by Dieter Deyke <deyke@mdddhd> on Fri May 31 10:08:16 1991
- #
- # This archive contains:
- # trunc.c
- #
- # Error checking via wc(1) will be performed.
- # Error checking via sum(1) will be performed.
-
- LANG=""; export LANG
- PATH=/bin:/usr/bin:$PATH; export PATH
-
- if sum -r </dev/null >/dev/null 2>&1
- then
- sumopt='-r'
- else
- sumopt=''
- fi
-
- echo x - trunc.c
- cat >trunc.c <<'@EOF'
- /* trunc - truncate files
-
- trunc [ options ] file ...
-
- -m USER mail file to USER before truncating or removing
- (done only if the file is not empty).
-
- -b BYTES keep only the last BYTES bytes of the file.
-
- -r remove file (useful with -m option).
-
- -o OWNER change owner of file to OWNER (must be numeric).
-
- -g GROUP change group of file to GROUP (must be numeric).
-
- -c MODE change mode of file to MODE.
-
- */
-
- static char sccsid[] = "@(#) trunc.c 1.2 87/03/10 21:32:16";
-
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #define BUFSIZE 8*1024
- #define MAXINT 0x7fffffff
-
- /*---------------------------------------------------------------------------*/
-
- void mail_file_to_user(filename, user)
- char *filename, *user;
- {
-
- FILE * fpin, *fpout;
- char cmdline[256];
- int c;
-
- if (!(fpin = fopen(filename, "r"))) return;
- sprintf(cmdline, "/bin/mail %s", user);
- if (!(fpout = popen(cmdline, "w"))) return;
- fprintf(fpout, "To: %s\nSubject: %s\n\n", user, filename);
- while ((c = getc(fpin)) != EOF) putc(c, fpout);
- fclose(fpin);
- pclose(fpout);
- }
-
- /*---------------------------------------------------------------------------*/
-
- void truncate_file(filename, bytes)
- char *filename;
- int bytes;
- {
-
- extern long lseek();
-
- char buffer[BUFSIZE];
- char tempfile[256];
- int fi;
- int fo;
- unsigned i;
-
- sprintf(tempfile, "/tmp/trunc%d", getpid());
-
- fi = open(filename, O_RDONLY);
- fo = creat(tempfile, 0600);
- lseek(fi, (long) -bytes, 2);
- while ((i = read(fi, buffer, BUFSIZE)) > 0)
- write(fo, buffer, i);
- close(fi);
- close(fo);
-
- fi = open(tempfile, O_RDONLY);
- fo = creat(filename, 0666);
- while ((i = read(fi, buffer, BUFSIZE)) > 0)
- write(fo, buffer, i);
- close(fi);
- close(fo);
-
- unlink(tempfile);
- }
-
- /*---------------------------------------------------------------------------*/
-
- main(argc, argv)
- int argc;
- char **argv;
- {
-
- extern char *optarg;
- extern int optind;
- extern long strtol();
- extern void exit();
-
- char *filename;
- char *user = 0;
- int bytes = MAXINT;
- int c;
- int errflag = 0;
- int group;
- int mode;
- int owner;
- int remove = 0;
- int set_group = 0;
- int set_mode = 0;
- int set_owner = 0;
- struct stat statbuf;
-
- while ((c = getopt(argc, argv, "m:b:ro:g:c:")) != EOF)
- switch (c) {
- case 'm':
- user = optarg;
- break;
- case 'b':
- bytes = atoi(optarg);
- break;
- case 'r':
- remove = 1;
- break;
- case 'o':
- owner = atoi(optarg);
- set_owner = 1;
- break;
- case 'g':
- group = atoi(optarg);
- set_group = 1;
- break;
- case 'c':
- mode = strtol(optarg, 0, 8);
- set_mode = 1;
- break;
- case '?':
- errflag = 1;
- break;
- }
-
- if (errflag) {
- fprintf(stderr, "usage: trunc [-m user] [-b bytes] [-r] [-o owner] [-g group] [-c mode] file ...\n");
- return 2;
- }
-
- for (; optind < argc; optind++) {
- filename = argv[optind];
-
- if (stat(filename, &statbuf)) continue;
- if (set_group && !set_owner) owner = statbuf.st_uid;
- if (set_owner && !set_group) group = statbuf.st_gid;
-
- if (user && statbuf.st_size) mail_file_to_user(filename, user);
-
- if (remove) {
- unlink(filename);
- continue;
- }
- if (bytes <= 0)
- close(creat(filename, 0644));
- else if (bytes < statbuf.st_size)
- truncate_file(filename, bytes);
-
- if (set_mode) chmod(filename, mode);
- if (set_owner || set_group) chown(filename, owner, group);
- }
- exit(0);
- return 0;
- }
- @EOF
- set `sum $sumopt <trunc.c`; if test $1 -ne 38042
- then
- echo ERROR: trunc.c checksum is $1 should be 38042
- fi
- set `wc -lwc <trunc.c`
- if test $1$2$3 != 1664403578
- then
- echo ERROR: wc results of trunc.c are $* should be 166 440 3578
- fi
-
- chmod 644 trunc.c
-
- exit 0
-
- mailx>